﻿using M.Warehouse.API.Application.Command.BinLocationsCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.Warehouse.API.Controllers
{
    /// <summary>
    /// 库位信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BinLocationsController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public BinLocationsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示仓库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Warehouses>>> GetWarehousesList()
        {
            ApiResult<List<Warehouses>> result = new ApiResult<List<Warehouses>>();
            try
            {
                result.Data = await _mediator.Send(new WareShowCommand());

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库区信息（二级联动）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Reservoirs>>> GetReservoirsList([FromQuery]ReseShowCommand command)
        {
            ApiResult<List<Reservoirs>> result = new ApiResult<List<Reservoirs>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加库位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateBinLocations(BinLocationsCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加库位信息失败";
                throw;
            }
            return result;

        }
        /// <summary>
        /// 显示库位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<WarehBinDto>>> GetBinLocationsList([FromQuery] BinLocationsListCommand command)
        {
            ApiResult<PageDTO<WarehBinDto>> result = new ApiResult<PageDTO<WarehBinDto>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除库位信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteBinLocations([FromQuery] BinLocationsDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "单独删除库位信息失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填库位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<BinLocations>> HandleBinLocations([FromQuery] BinLocationsByidCommand command)
        {
            ApiResult<BinLocations> result = new ApiResult<BinLocations>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改库位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateBinLocations(BinLocationsUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改库位信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库位根据仓库id
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<BinLocations>>> BinLocationsShowByReservoirId([FromQuery] BinLocationsShowByReservoirIdCommand command)
        {
            ApiResult<List<BinLocations>> result = new ApiResult<List<BinLocations>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库位根据库区id
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<BinLocations>>> BinLocationsShowByRId([FromQuery] BinlocationsShowByRIdCommand command)
        {
            ApiResult<List<BinLocations>> result = new ApiResult<List<BinLocations>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
